package com.cipe.cmrs.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.cipe.cmrs.model.ChangeNotificationsMessage;
import com.cipe.cmrs.model.Changenotification;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.model.Useractionalertschanges;

/**
 * @author krishna The service class to Change Notification message message
 */
@Service
public class ChangeNotificationService extends BaseService<ChangeNotificationsMessage> {
	/**
	 * We are taking child object as EntityManager won't support Hibernate Filter concept
	 * @param user
	 * @return
	 */
	public List<ChangeNotificationsMessage> getAllChangeNotification(User user) {
		StringBuilder ids=new StringBuilder("99999999");
		for (Useractionalertschanges useractionalertschanges : user
				.getUseractionalertschangesList())
			ids.append(","+useractionalertschanges.getChnagenotificationid());
	
		return this.dao
				.getResultList("select o from ChangeNotificationsMessage o where o.id not in("+ids+")");

	}
	public void chnageNotificationPeference(User user,int id){
		Useractionalertschanges useractionalertschanges=new Useractionalertschanges();
		useractionalertschanges.setChnagenotificationid(id);
		useractionalertschanges.setDateread(new Timestamp(new Date().getTime()));
		useractionalertschanges.setUser(user);
		user.getUseractionalertschangesList().add(useractionalertschanges);
	}
}
